package system;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements Operation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        Scanner scan = new Scanner(System.in) ;
        System.out.println("输入你要删除的图书的书名");
        String name = scan.nextLine() ;
        int ret = -1 ;
        int currentSize = bookList.getUsedSize();
        for(int i = 0 ;i < currentSize ; i ++){
            Book book = bookList.getBook(i) ;
            if (book.getName().equals(name)) {
                ret = i ;
            }
        }
        if(ret == -1 ){
            System.out.println("没有你要删除的图书");
        }else{
            for (int i = ret; i < bookList.getUsedSize() - 1; i++) {
                bookList.setBook(ret , bookList.getBook(ret+1));
            }
            bookList.setUsedSize(bookList.getUsedSize() - 1 );
            System.out.println("删除成功");
        }
    }
}
